home *** CD-ROM | disk | FTP | other *** search
/ Amiga Games Extra 1996 September / Amiga Games Extra CD-ROM 9-1996.iso / spiele / publicdomain / elan / src / amiga / console.c next >
C/C++ Source or Header  |  1996-06-07  |  4KB  |  188 lines

  1.  
  2. #include "global.h"
  3. #include "console.h"
  4.  
  5. #include <clib/exec_protos.h>
  6. #include <clib/alib_protos.h>
  7. #include <proto/intuition.h>
  8.  
  9. #define WRITE_PORT_NAME        "con_output"
  10. #define READ_PORT_NAME        "con_input"
  11.  
  12. struct Window *win;
  13. struct NewWindow win_desc =
  14.     {
  15.     0, 0,        /* Start pos */
  16.     640, 480,    /* Size */
  17.     -1, -1,        /* Pens */
  18.     0,            /* IDCMP flags */
  19.     WINDOWDEPTH | WINDOWDRAG | SMART_REFRESH | ACTIVATE,    /* Flags */
  20.     NULL,        /* Gadgets */
  21.     NULL,        /* Checkmarks */
  22.     NULL,        /* Title */
  23.     NULL,        /* Custom screen */
  24.     NULL,        /* Super-bitmap */
  25.     0, 0,        /* Min size */
  26.     0, 0,        /* Max size */
  27.     WBENCHSCREEN
  28.     };
  29.  
  30. struct IOStdReq *write_msg;
  31. struct IOStdReq *read_msg;
  32. struct MsgPort *write_port;
  33. struct MsgPort *read_port;
  34.  
  35. char input_char;
  36. boolean async_io;
  37. colors_pair pen_colors[PEN_NUMBER];
  38.  
  39. error_class open_console(const char *title_string, int x, int y, int l, int h)
  40.     {
  41.     if(!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 0)))
  42.         return(NULL);
  43.  
  44.     win_desc.LeftEdge = x;
  45.     win_desc.TopEdge = y;
  46.     win_desc.Width = l;
  47.     win_desc.Height = h;
  48.     win_desc.Title = (char *)title_string;
  49.  
  50.     if(!(win = OpenWindow(&win_desc)))
  51.         return(console_err);
  52.  
  53.     if(!(write_port = CreatePort(WRITE_PORT_NAME, 0)))
  54.         return(console_err);
  55.  
  56.     if(!(read_port = CreatePort(READ_PORT_NAME, 0)))
  57.         return(console_err);
  58.  
  59.     if(!(write_msg = CreateStdIO(write_port)))
  60.         return(console_err);
  61.  
  62.     if(!(read_msg = CreateStdIO(read_port)))
  63.         return(console_err);
  64.  
  65.     write_msg->io_Data = (APTR)win;
  66.     write_msg->io_Length = sizeof(*win);
  67.  
  68.     if(OpenDevice("console.device", 0, (struct IORequest *)write_msg, 0))
  69.         return(console_err);
  70.  
  71.     read_msg->io_Device = write_msg->io_Device;
  72.     read_msg->io_Unit = write_msg->io_Unit;
  73.  
  74.     return ok;
  75.     }
  76.  
  77. void close_console()
  78.     {
  79.     CloseDevice((struct IORequest *)write_msg);
  80.  
  81.     DeleteStdIO(write_msg);
  82.     DeleteStdIO(read_msg);
  83.  
  84.     DeletePort(write_port);
  85.     DeletePort(read_port);
  86.  
  87.     CloseWindow(win);
  88.  
  89.     CloseLibrary((struct Library *)IntuitionBase);
  90.     }
  91.  
  92. void write_char(char output_char)
  93.     {
  94.     write_msg->io_Command = CMD_WRITE;
  95.     write_msg->io_Data = &output_char;
  96.     write_msg->io_Length = 1;
  97.  
  98.     DoIO((struct IORequest *)write_msg);
  99.     }
  100.  
  101. char read_char()
  102.     {
  103.     if(!async_io)
  104.         {
  105.         read_msg->io_Command = CMD_READ;
  106.         read_msg->io_Data = &input_char;
  107.         read_msg->io_Length = 1;
  108.  
  109.         DoIO((struct IORequest *)read_msg);
  110.         }
  111.     else
  112.         {
  113.         async_io = FALSE;
  114.         WaitIO((struct IORequest *)read_msg);
  115.         }
  116.  
  117.     return input_char;
  118.     }
  119.  
  120. char async_read_char()
  121.     {
  122.     if(!async_io)
  123.         {
  124.         async_io = TRUE;
  125.         read_msg->io_Command = CMD_READ;
  126.         read_msg->io_Data = &input_char;
  127.         read_msg->io_Length = 1;
  128.  
  129.         SendIO((struct IORequest *)read_msg);
  130.         }
  131.  
  132.     if(CheckIO((struct IORequest *)read_msg))
  133.         {
  134.         async_io = FALSE;
  135.         return input_char;
  136.         }
  137.     else
  138.         return '\0';
  139.     }
  140.  
  141. void write_string(const char *string)
  142.     {
  143.     write_msg->io_Command = CMD_WRITE;
  144.     write_msg->io_Data = (char *)string;
  145.     write_msg->io_Length = -1;
  146.  
  147.     DoIO((struct IORequest *)write_msg);
  148.     }
  149.  
  150. void con_printf(const char *format_string, ...)
  151.     {
  152.     char buffer[STRING_MAX_LEN];
  153.     va_list arg_list;
  154.  
  155.     va_start(arg_list, format_string);
  156.     vsprintf(buffer, format_string, arg_list);
  157.     write_string(buffer);
  158.     va_end(arg_list);
  159.     }
  160.  
  161. int register_pen(colors_pair pair)
  162.     {
  163.     static int last_pen;
  164.     int pen;
  165.  
  166.     for(pen = 0; pen < last_pen; pen++)
  167.         if(pen_colors[pen].fg == pair.fg && pen_colors[pen].bg == pair.bg)
  168.             return pen;
  169.  
  170.     pen_colors[last_pen] = pair;
  171.     pen_colors[reverse_pen(last_pen)].fg = pair.bg;
  172.     pen_colors[reverse_pen(last_pen)].bg = pair.fg;
  173.  
  174.     return last_pen++;
  175.     }
  176.  
  177. void set_pen(int pen)
  178.     {
  179.     static int prev_pen;
  180.  
  181.     if(pen != prev_pen)
  182.         {
  183.         prev_pen = pen;
  184.         con_printf(SET_COLORS("%d", "%d"), pen_colors[pen].fg, pen_colors[pen].bg);
  185.         }
  186.     }
  187.  
  188.